package com.hp.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hp.context.Config;
import com.hp.dao.BookDao;
import com.hp.entity.books;
import com.hp.result.DataGridResultInfo;
import com.hp.result.ResultUtil;
import com.hp.result.SubmitResultInfo;
import com.hp.service.BookService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional
@Service

public class BookServiceImpl implements BookService {
    @Autowired
    private BookDao bookDao;

    @Transactional(readOnly = true)
    @Override
    public List<books> findAll() throws Exception {
        return bookDao.findAll();
    }

    @Transactional(readOnly = true)
    @Override
    public DataGridResultInfo findlinkList(int page, int rows) throws Exception {
        Page<books> startPage = PageHelper.startPage(page, rows);//使用分页插件方法
        bookDao.findAll();//插件方法必须在查询数据之前
        //封装数据
        DataGridResultInfo dataGridResultInfo = new DataGridResultInfo();
        dataGridResultInfo.setRows(startPage.getResult());//当前查询的数据
        dataGridResultInfo.setTotal((int) startPage.getTotal());//总页数
        return dataGridResultInfo;
    }

    /**
     * 删除
     * @param * @return
     * @throws Exception
     */
    @Override
    public SubmitResultInfo deletetype(String booktypeid) throws Exception {
        bookDao.deleteById(booktypeid);
        return ResultUtil.createSubmitResult(ResultUtil.createSuccess(Config.MESSAGE,662,new Object[]{"类型"}));
    }

    /**
     * 修改
     * @param book
     * @return
     * @throws Exception
     */
    @Override
    public SubmitResultInfo updateLink(books book) throws Exception {
        bookDao.update(book);
        return ResultUtil.createSubmitResult(ResultUtil.createSuccess(Config.MESSAGE,666,new Object[]{""}));
    }

    @Override
    public SubmitResultInfo add(books book) throws Exception {
        bookDao.add(book);
        return  ResultUtil.createSubmitResult(ResultUtil.createSuccess(Config.MESSAGE,661,new Object[]{""}));
    }

    /**
     * 修改类型
     * @param
     * @return
     * @throws Exception
     */
    @Override
    public books findById(String bookid) throws Exception {
        return bookDao.findByIdLink(bookid);
    }

}
